package de.mmis.utilities.genericPublisher;

import de.mmis.core.base.infrastructure.PublishedDeviceDescription;
import de.mmis.core.pojop.server.Server;
import de.mmis.core.tuplespace.TuplespacePublisher;

/**
 * Stores information about the publisher.<br>
 * <p>
 * <b>Important note for subclasses:</b> Any client should not be able to modify
 * the publish state of the object with this handle directly. This implies for
 * example that there should be no public field like {@link Server} or
 * {@link TuplespacePublisher}. This is just a <em>handle</em>.
 * </p>
 * 
 * @author Martin Nyolt
 * 
 */
public abstract class PublishHandle {

	protected final String id;
	protected final PublishedDeviceDescription description;

	private boolean closed;

	public PublishHandle(String id, PublishedDeviceDescription description) {
		this.id = id;
		this.description = description;
	}

	public PublishedDeviceDescription getDeviceDescription() {
		return description;
	}

	/**
	 * @return true if the device is not published any longer (i.e.
	 *         GenericPublisher.unpublish() was called)
	 */
	public boolean isClosed() {
		return closed;
	}

	/**
	 * further calls to isClosed() will return true.
	 */
	void setClosed() {
		closed = true;
	}

	/**
	 * print all information on a single line.<br />
	 * Use prettyString() to get a pretty multi-line output
	 */
	@Override
	abstract public String toString();

	/**
	 * @return a pretty formatted String, including the
	 *         {@link PublishedDeviceDescription}, terminated by a newline
	 */
	public String prettyPrint() {
		String result = "Published " + id + "\n";
		result += "\t" + prettyStringImpl();

		if (description != null)
			result += "\tDevice Description: " + description.toString() + "\n";

		return result;
	}

	/**
	 * must return a pretty formatted String, including only subclass-specific
	 * information.<br/>
	 * pretty format means:
	 * <ul>
	 * <li>first, general information line</li>
	 * <li>detailed information line by line, preceded by a tab</li>
	 * <li>last line terminated by a newline</li>
	 * </ul>
	 */
	abstract protected String prettyStringImpl();
}